package com.frame.work.frame.deal.device0faa;

import com.alibaba.fastjson.JSONObject;
import com.frame.work.common.constant.TypeEnum;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.config.SpringUtil;
import com.frame.work.entity.DeviceAssociatedInformation;
import com.frame.work.entity.TblSecurityLog;
import com.frame.work.frame.deal.base.BaseFrame;
import com.frame.work.mapper.SelectDeviceMapper;
import com.frame.work.service.TblSecurityLogService;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 声光报警器
 */
@Data
public class ShengGuangBaoJingQi_0FAA0902 extends BaseFrame {

    private String switchState;

    public ShengGuangBaoJingQi_0FAA0902(String gatewayMac, String data) {

        //将基础数据进行处理
        super(gatewayMac, data);

        //处理此设备特有的数据
        this.setSwitchState(StringUtils.substring(data, data.length() - 2, data.length()));

        //数据库操作（根据状态进行数据库操作）
        SelectDeviceMapper testMapper = (SelectDeviceMapper) SpringUtil.getBean(SelectDeviceMapper.class);
        List<DeviceAssociatedInformation> list = testMapper.getDevice1(this.getDeviceMac());
        String type = TypeEnum.NORMAL.code();
        if (list.size() > 0) {
            DeviceAssociatedInformation deviceRelation = list.get(0);

            String deviceName = deviceRelation.getDeviceName();
            if (deviceName == "" || deviceName == null) {
                deviceName = "声光警报器";
            }
            String content = deviceName + "触发警报";
            type = TypeEnum.WARNING.code();
           /* String orgCode = (String) list.get(0).get("organization_code");
            String account = (String) list.get(0).get("account");
            String nickName = (String) list.get(0).get("nick_name");*/
            //保存报警信息
            TblSecurityLog tblSecurityLog = new TblSecurityLog();
            tblSecurityLog.setGatewayMac(this.getGatewayMac());
            tblSecurityLog.setDeviceMac(this.getDeviceMac());
            tblSecurityLog.setContent(content);
            //获取userId
            String userId = deviceRelation.getUserId();
            tblSecurityLog.setUserId(userId);
            tblSecurityLog.setType(type);
            TblSecurityLogService tblSecurityLogService = (TblSecurityLogService) SpringUtil.getBean(TblSecurityLogService.class);
            tblSecurityLogService.insert(tblSecurityLog);

        }


        //将actions信息放入json对象中
        Map<String, Object> actions = new HashMap();
        actions.put("switchState", switchState);
        this.setActions(actions);


        //处理好的json对象
        String json = JSONObject.toJSONString(this);
        //将数据发送到mqtt队列中
        JsonToMqtt.sendMQTT(gatewayMac, json);

    }

}
